﻿var UPDATE_FIELD = "#ff0000";
var NORMAL_FIELD = "#000000";
var DELETE_LINE  = "lightgrey";
var NORMAL_LINE  = "white";
var DELPLAN_LINE  = "lavender";
var NORPLAN_LINE  = "snow";
var SELECT_LINE  = "yellow";

function setDisplay() {
	var idx = setDisplay.arguments[0];
	for (var i = 1; i <= setDisplay.arguments.length - 1; i++) {
		var disable = setDisplay.arguments[i];
		var obj = document.getElementById(disable);
		if ( obj == null ) continue;
		try{
			if ( (i-1) != idx ){
				obj.style.display = "none";
			}else{
				obj.style.display = "inline";
			}
		}catch(ex){

		}
	}
}


function rotateDisplay() {
	var display = rotateDisplay.arguments[0];
	if ( display == "inline" ){
	}else if ( display == "block" ){
	}else{
		display="inline";
	}
	for (var i = 0; i <= rotateDisplay.arguments.length - 1; i++) {
		var disable = rotateDisplay.arguments[i];
		var obj = document.getElementById(disable);
		if ( obj == null ) continue;
		try{
			if ( obj.style.display == "none" ){
				obj.style.display = display;
			}else{
				obj.style.display = "none";
			}
		}catch(ex){

		}
	}
}


/**
 * SystemLog Class
 **/
var CSystemLog = function(target){
	var strUrl = location.href;
	strUrl = strUrl.replace("http://","");
	var fpt = strUrl.indexOf("/",0);
	var spt = strUrl.indexOf("/",fpt+1);
	strUrl = strUrl.substring(fpt,spt);

	this.target = target;
	this.xmlHttp = this.getXmlHttp();
	this.serverURL = strUrl+"/systemlog.log0000.fw";
	this.requestNo = 0;
	var me = this;
	if ( target != null ){
		this.timerID = setTimeout(function(){me.onTimer();},2000);
	}
}

CSystemLog.prototype.getXmlHttp = function(){
	var xmlHttp = null;
	if(window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
};

CSystemLog.prototype.doLoadData = function(){
	var ajax = this;
	this.requestNo = this.requestNo + 1;
	var now = new Date();

	var year1 = now.getYear(); // 年
	var month1 = now.getMonth() + 1; // 月
	var day1 = now.getDate(); // 日
	var hour1 = now.getHours(); // 時
	var min1 = now.getMinutes(); // 分
	var sec1 = now.getSeconds(); // 秒
	var strDate = year1+"/"+month1+"/"+day1+"-"+hour1+":"+min1+":"+sec1;
	var url = this.serverURL + '?datetime=' + strDate;
	ajax.xmlHttp.open("POST",url);
	ajax.xmlHttp.onreadystatechange = function() {
		ajax.onReadyStateChange(ajax.xmlHttp);
	}
	ajax.xmlHttp.send(null);
};

CSystemLog.prototype.onReadyStateChange = function(ajax){
	if (ajax.readyState == 4 && ajax.status == 200) {
		var obj = document.getElementById(this.target);

		var xml = ajax.responseXML;
		var list = xml.getElementsByTagName("list");
		if ( list == null ) return;
		if ( list.length <= 0 ) return;
		var root = list[0];
		var logs = root.getElementsByTagName("log");
		var msg = "";
		for ( var i=0; i<logs.length; i++ ){
			var log = logs[i];
			var date = getElementByTagName(log,"date");
			var result = getElementByTagName(log,"result");
			var style = getElementByTagName(log,"style");
			var error = getElementByTagName(log,"error");
			var topic = getElementByTagName(log,"topic");
			var contents = getElementByTagName(log,"text");
			var clsName = null;
			if ( getValue(style)=="ERR" ){
				clsName="log-error";
			}else{
				clsName="log-normal";
			}

			var text = "<div class='"+clsName+"'>";
			text += "<span class='date'>"+getValue(date)+"</span>";
			text += "<span class='result'>"+getValue(result)+" "+getValue(error)+"</span>";
			text += "<span class='topic'>"+getValue(topic)+"</span>";
			text += "<span class='text'>"+getValue(contents)+"</span>\n";
			text += "</div>";

			msg += text;
		}

		obj.innerHTML = msg;
	}
};

CSystemLog.prototype.onTimer = function(){
	this.doLoadData();
	clearTimeout(this.timerID);
	var me = this;
	this.timerID = setTimeout(function(){me.onTimer();},60000);
};

function getElementByTagName(root,tagName){
	try{
		if ( root != null ){
			var node = root.getElementsByTagName(tagName);
			if ( node != null ){
				if ( node.length > 0 ) return node[0];
			}
		}
	}catch(ex){
	}
	return null;
}

function getValue(node){
	try{
		if ( node != null ){
			var chiles = node.childNodes;
			if ( chiles != null ){
				if ( chiles.length > 0 ) return chiles[0].nodeValue;
			}
		}
	}catch(ex){
	}
	return null;
}

/*
 * URLEncode/URLDecode
 */
function URLEncode(src) {
	var ret = src;

	try{
		ret = ret.replace(/\+/g, "-");
		ret = ret.replace(/\//g, "_");
//		ret = ret.replace(/=/g,  "~");
	}catch(ex){
	}

	return ret;
}
function URLDecode(src) {
	var ret = src;

	try{
		ret = ret.replace(/-/g, "+");
		ret = ret.replace(/_/g, "/");
//		ret = ret.replace(/~/g, "=");
	}catch(ex){
	}

	return ret;
}
